package com.sihan.framework.etm.controller;

import com.framework.annotation.BusinessLog;
import com.framework.model.ApiResult;
import com.framework.util.lang.RegexpUtils;
import com.sihan.framework.etm.dto.ResetPsdDTO;
import com.sihan.framework.etm.dto.UserAddDTO;
import com.sihan.framework.etm.dto.UserInfoDTO;
import com.sihan.framework.etm.dto.UserRoleDTO;
import com.sihan.framework.etm.dto.UserUpdateDTO;
import com.sihan.framework.etm.entity.User;
import com.sihan.framework.etm.service.UserService;
import com.sihan.framework.etm.vo.UserVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/system/v0/user")
public class AdminUserController {

  @Autowired
  private UserService userService;

  @GetMapping("/list")
  @ApiOperation(value = "用户查询", notes = "用户查询", response = UserVO.class)
  @ApiImplicitParams({
      @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
      @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true),
      @ApiImplicitParam(name = "orderBy", value = "排序字段", dataType = "string", paramType = "query"),
      @ApiImplicitParam(name = "name", value = "用户名称", dataType = "string", paramType = "query"),
      @ApiImplicitParam(name = "role", value = "角色", dataType = "string", paramType = "query"),
      @ApiImplicitParam(name = "code", value = "工号", dataType = "string", paramType = "query")
  })
  @BusinessLog("查询用户")
  public ApiResult<?> listUsers(UserInfoDTO dto) {
      return ApiResult.success(userService.pageUser(dto));
  }

  @PostMapping("/add")
  @ApiOperation("添加用户")
  @BusinessLog("添加用户")
  public ApiResult<?> addUser(@Valid @ApiParam(name = "userDto", value = "用户新增数据", required = true) @RequestBody UserAddDTO userDto) {
    User user = new User();
    BeanUtils.copyProperties(userDto, user);
    userService.addUser(user, userDto.getRoleIds(),userDto.getOrgId());
    return ApiResult.success();
  }

  @PostMapping("/update")
  @ApiOperation("更新用户")
  @BusinessLog("更新用户")
  public ApiResult<?> editUser(@Valid @ApiParam(name = "userDto", value = "用户修改数据", required = true) @RequestBody UserUpdateDTO userDto) {
    User user = new User();
    BeanUtils.copyProperties(userDto, user);
    userService.updateUser(user, userDto.getRoleIds(),userDto.getOrgId());
    return ApiResult.success();
  }

  @PostMapping("/delete")
  @ApiOperation("删除用户")
  @BusinessLog("删除用户")
  public ApiResult<?> deleteUser(@ApiParam(name = "userIds", value = "用户Ids", required = true) @RequestBody List<String> userIds) {
    userService.deleteUserByUserIds(userIds);
    return ApiResult.success();
  }


  @PostMapping("/active")
  @ApiOperation("激活用户")
  @BusinessLog("激活用户")
  public ApiResult<?> activeUser(@ApiParam(name = "userId", value = "用户Id", required = true) @RequestParam String userId) {
    userService.activeUserByUserId(userId);
    return ApiResult.success();
  }

  @PostMapping("/unlock")
  @ApiOperation("解锁用户")
  @BusinessLog("解锁用户")
  public ApiResult<?> unlockUser(@ApiParam(name = "userId", value = "用户Id", required = true) @RequestParam String userId) {
    userService.unlockUserByUserId(userId);
    return ApiResult.success();
  }

  @ApiOperation("重置密码")
  @BusinessLog("重置密码")
  @PostMapping("/resetpassword")
  public ApiResult<?> resetPassword(@Valid @RequestBody ResetPsdDTO dto) {
    String newPsd = dto.getNewPassword();
    String suNewPsd = dto.getSuNewPassword();
    if(!RegexpUtils.isPassword(newPsd) || !RegexpUtils.isPassword(suNewPsd)) {
      return ApiResult.fail("密码只能是8-20位必须符合由大写字母、小写字母、数字、特殊字符至少其中三种组成");
    } else {
      if(!newPsd.equals(suNewPsd)) {
        return ApiResult.fail("新密码和确认密码不一致");
      }

      userService.resetPassword(dto.getId(), newPsd);
      return ApiResult.success();
    }
  }

  @ApiOperation("用户鉴别")
  @BusinessLog("用户鉴别")
  @GetMapping("/identuser")
  public ApiResult<?> identuser(@ApiParam(name = "password", value = "密码", required = true) @RequestParam String password) {
    boolean success = userService.identuser(password);
    return success ? ApiResult.success() : ApiResult.fail("鉴别失败");

  }

  @ApiOperation("分配角色")
  @PostMapping("/role/update")
  @BusinessLog(value = "分配角色")
  public ApiResult<?> saveRolePermission(@Valid @ApiParam(name = "dto", value = "分配角色数据", required = true) @RequestBody UserRoleDTO dto) {
	  userService.upUserRole(dto.getRoleIds(),dto.getId());
      return ApiResult.success();
  }

  @PostMapping("/list/byCompany")
  @ApiOperation("根据公司查询人员")
  @BusinessLog("根据公司查询人员")
  public ApiResult<?> getUserList(@ApiParam(name = "companyId", value = "公司Id", required = true) @RequestParam String companyId) {
	  List<Map<String, String>> result = userService.getUserList(companyId);
      return ApiResult.success(result);
  }
}
